package com.example.servlet;

import com.alibaba.fastjson2.JSON;
import com.example.service.PetService;
import com.example.service.impl.PetServiceImpl;
import com.example.util.AjaxResult;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/deletePet")
public class DeletePetServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String petId = req.getParameter("petId");
        resp.setCharacterEncoding("UTF-8");
        PrintWriter pw = resp.getWriter();
        if(StringUtils.isNotBlank(petId)){
            PetService petService = new PetServiceImpl();
            int result = petService.delete(Integer.parseInt(petId));
            if(result > 0){
                pw.print(JSON.toJSONString(AjaxResult.success()));
            }else{
                pw.print(JSON.toJSONString(AjaxResult.error()));
            }
        }else{
            pw.print(JSON.toJSONString(AjaxResult.error("参数不合法!")));
        }
        pw.flush();
        pw.close();
    }
}
